package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.MoveDownWarehouse;
import com.wing.warehouse.model.entity.MoveDownWarehouseIterm;
import com.wing.warehouse.model.request.MoveDownWarehouseItermForm;
import com.wing.warehouse.model.response.MoveDownWarehouseItermVO;
import com.wing.warehouse.service.MoveDownWarehouseItermService;
import com.wing.warehouse.service.MoveDownWarehouseService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-调拨任务详情表管理", tags = "#/warehouse/moveDownWarehouseIterm")
@RestController("moveDownWarehouseItermController")
@RequestMapping("warehouse/admin/moveDownWarehouseIterm")
@RequiredArgsConstructor
public class MoveDownWarehouseItermController {

    private final MoveDownWarehouseItermService moveDownWarehouseItermService;
    @Autowired
    private MoveDownWarehouseService moveDownWarehouseService;
    
    @ApiOperation(value = "查询调拨任务明细列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<MoveDownWarehouseItermVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String moveDownItermNumber
            ,String moveDownNumber
            ,Long outWarehouseId
            ,Long inWarehouseId
            ,String creatorName
            ,String startCreateTime
            ,String endCreateTime
    ){
        Page<MoveDownWarehouseIterm> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("moveDownItermNumber", moveDownItermNumber);
        params.put("moveDownNumber", moveDownNumber);
        params.put("outWarehouseId", outWarehouseId);
        params.put("inWarehouseId", inWarehouseId);
        params.put("creatorName", creatorName);
        params.put("startCreateTime", startCreateTime);
        params.put("endCreateTime", endCreateTime);
        //排序
        params.put("sort", sort);
        params.put("order", order);
        Page<MoveDownWarehouseItermVO> list = moveDownWarehouseItermService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加移库下货详情表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(MoveDownWarehouseItermForm.Add.class) MoveDownWarehouseItermForm moveDownWarehouseItermForm) {
        if (moveDownWarehouseItermService.add(moveDownWarehouseItermForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改移库下货详情表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(MoveDownWarehouseItermForm.Update.class) MoveDownWarehouseItermForm moveDownWarehouseItermForm) {
        MoveDownWarehouseIterm moveDownWarehouseIterm = moveDownWarehouseItermService.getById(moveDownWarehouseItermForm.getId());
        if(moveDownWarehouseIterm == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(MoveDownWarehouseIterm.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(moveDownWarehouseItermForm, moveDownWarehouseIterm, copyOptions);
        if (moveDownWarehouseItermService.updateById(moveDownWarehouseIterm)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "修改移库下货详情表", notes = "修改")
    @PutMapping("/updateByMap")
    public JsonResult updateByMap(@RequestBody Map<String, Object> param) {
        MoveDownWarehouse moveDownWarehouse = moveDownWarehouseService.getById(Long.parseLong(param.get("id").toString()));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("move_down_number", moveDownWarehouse.getMoveDownNumber());
        queryWrapper.eq("sku_id", param.get("skuId"));
        MoveDownWarehouseIterm moveDownWarehouseIterm = moveDownWarehouseItermService.getOne(queryWrapper);
        if(moveDownWarehouseIterm == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
            .setEditable(MoveDownWarehouseIterm.class)
            .setIgnoreError(true)
            .setIgnoreNullValue(true)
            .setIgnoreProperties("id");
        BeanUtil.copyProperties(param, moveDownWarehouseIterm, copyOptions);
        if (moveDownWarehouseItermService.updateById(moveDownWarehouseIterm)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }
    

    @ApiOperation(value = "删除移库下货详情表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moveDownWarehouseItermId", value = "移库下货详情表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long moveDownWarehouseItermId) {
        if (moveDownWarehouseItermService.removeById(moveDownWarehouseItermId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
